package com.tsintergy.tmosguide.tmos.serviceimpl.facade.impl;

import com.tsieframework.core.base.domain.aggregator.BeanAggregator;
import com.tsieframework.core.base.domain.aggregator.BeanListWrapper;
import com.tsintergy.tmosguide.casemng.serviceapi.facade.api.CaseDataFacadeService;
import com.tsintergy.tmosguide.casemng.serviceapi.facade.api.dto.CaseClrUnitPowerDTO;
import com.tsintergy.tmosguide.casemng.serviceapi.facade.api.dto.CaseUnitPowerFcDTO;
import com.tsintergy.tmosguide.tmos.serviceapi.facade.api.ClearAnalysisFacadeService;
import com.tsintergy.tmosguide.tmos.serviceapi.facade.dto.UnitPowerDetailDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("clearAnalysisFacadeService")
public class ClearAnalysisFacadeServiceImpl implements ClearAnalysisFacadeService {

    @Autowired
    CaseDataFacadeService caseMngFacadeService;

    @Override
    public List<UnitPowerDetailDTO> queryUnitPowerDetailList(String caseId, List<String> unitIdList) {
        List<CaseUnitPowerFcDTO> caseUnitPowerFcDTOList = caseMngFacadeService.queryUnitPowerFcList(caseId, unitIdList);
        List<CaseClrUnitPowerDTO> caseUnitPowerDTOList = caseMngFacadeService.queryClrUnitPowerList(caseId, unitIdList);

        List<BeanListWrapper> beanListWrapperList = new ArrayList<>();
        beanListWrapperList.add(BeanListWrapper.<CaseUnitPowerFcDTO, UnitPowerDetailDTO, String>of(caseUnitPowerFcDTOList, CaseUnitPowerFcDTO::getUnitId, ((bean, mergedBean) -> {
            mergedBean.setUnitId(bean.getUnitId());
            mergedBean.setUnitName(bean.getUnitName());
            mergedBean.setTvPowerFc(bean.getTvPower());
        })));
        beanListWrapperList.add(BeanListWrapper.<CaseClrUnitPowerDTO, UnitPowerDetailDTO, String>of(caseUnitPowerDTOList, CaseClrUnitPowerDTO::getUnitId, ((bean, mergedBean) -> {
            mergedBean.setUnitId(bean.getUnitId());
            mergedBean.setUnitName(bean.getUnitName());
            mergedBean.setTvPowerClr(bean.getTvPower());
            mergedBean.setTvPriceClr(bean.getTvPrice());
        })));
        //把unitId相同的 CaseUnitPowerFcDTO 和 CaseClrUnitPowerDTO 聚合成 UnitPowerDetailDTO
        BeanAggregator<UnitPowerDetailDTO> beanAggregator = BeanAggregator.of(UnitPowerDetailDTO.class);
        return beanAggregator.execute(beanListWrapperList);
    }

}
